﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_ucControl_ucNewMenuParent : System.Web.UI.UserControl
{
    TblParentMenuDAO parentMenuDAO = new TblParentMenuDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            if (id == null || id =="")
            {
                LoadMenuParent();
            }
            else
            {    
                LoadMenuParent();
                LoadDetailMenuParent();
            }
        }
    }

    private void LoadDetailMenuParent()
    {
        try
        {
            string id = Request.QueryString["id"];
            TblParentMenu menuPa = parentMenuDAO.GetMenuParentByID(Global.ConvertToInt(id));
            txtDescription.Content = Server.HtmlDecode(menuPa.MenuDescription);
            txttenungdung.Text = menuPa.MenuParentName;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    private void LoadMenuParent()
    {
        try
        {
            grdMenuParent.DataSource = parentMenuDAO.GetMenuParent();
            grdMenuParent.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];

            TblParentMenu parentMenu = new TblParentMenu();

            parentMenu.MenuDescription = Server.HtmlDecode(txtDescription.Content);
            parentMenu.MenuParentName = txttenungdung.Text;

            if (id == null || id == "")
            {
                parentMenu.IDMenu_Parent = 0;
                parentMenuDAO.InUpParentMenu(parentMenu, 1);
            }
            else
            {
                parentMenu.IDMenu_Parent = Global.ConvertToInt(id);
                parentMenuDAO.InUpParentMenu(parentMenu, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            LoadMenuParent();
        }

    }
  
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = true;
        try
        {
            TblParentMenu parentMenu = new TblParentMenu();

            parentMenu.IDMenu_Parent = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            parentMenu = parentMenuDAO.GetMenuParentByID(idXoa);
            parentMenuDAO.DeleteParentMenu(parentMenu);

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }

        if (flag)
        {
            LoadMenuParent();
        }
    }

}
